<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*******************
***  wish 刷新接口
***  
*** 2017/06/3
**********/

class Wishrefreshtoken
{
	
	function __construct()
	{    
		 $this->CI =& get_instance();
        $this->db1 = $this->CI->load->database('default', TRUE);
	}

public function getalltoken($id){
    ini_set('max_execution_time', '0');
   $row = $this->db1->get_where("d_shop","platfromname='Wish' and id ='".$id."'")->result_array();
  foreach ($row as $v) {
    $shopid=$id;
  	$name=$v['name'];
    $data=json_decode($v['amazonkey'],true);
    $client_secret=$data['wishsecret'];
    $refresh_token=$data[0]['refresh_token'];
    // $refresh_token='6727e43234684df1bb1797049e83f187';
    $client_id=$data[0]['client_id'];
    // $client_id='596d9b949415850ffdb8538e';
    //{"wishkey":"5926780bd594f14ad81d04e9","wishsecret":"70034ac1daa84f91bd8e429af71b543a","0":{"expiry_time":1499096809,"token_type ":"access_token","access_token":"a4f1e2db01034090ba781fd3710e4f8b","expires_in":2591959,"reason":null,"refresh_token":"cc1f16e5529349ed84e8448dccf28a8b","client_id":"5926780bd594f14ad81d04e9","token_id":"59327779f186cf1664b9fd9b","merchant_id":"59267398f186cf52ef48e716","merchant_user_id":"59267398f186cf52ef48e715"}}
    $url = sprintf(
    "https://merchant.wish.com/api/v2/oauth/refresh_token?&client_id=%s&client_secret=%s&refresh_token=%s&grant_type=refresh_token",
    $client_id, $client_secret, $refresh_token);

$context = stream_context_create(array(
    'http' => array(
        'method'        => 'POST',
        'ignore_errors' => true,
    ),
));

// Send the request
$response = file_get_contents($url, TRUE, $context);
//print_r($response);

// {
//     'message' : '',
//     'code' : 0,
//     'data' : { 'access_token' : '1qaz2wsx3edc4rfv5tgb',
//                'refresh_token' : 'mju7nhy6bgt5vfr4cde3',
//                'expires_in' : 86400,
//                'expiry_time' : 1438922740
//              }
// }
  // $shopid=141;
  // $data='{"wishkey":"596d9b949415850ffdb8538e","wishsecret":"f60a1ffacfde49b0821aeaf26b8797b8"}';
  // $data=json_decode($data,true);
// $response='{"message":"","code":0,"data":{"expiry_time":1504187245,"token_type ":"access_token","access_token":"61416130a5b14d6cbd3c4f114fb380a1","expires_in":2592000,"reason":null,"refresh_token":"6727e43234684df1bb1797049e83f187","client_id":"596d9b949415850ffdb8538e","token_id":"598023fd5420e57504f1f661","merchant_user_id":"5673a1c2c5271926ad4cb90c"}}';

$response=json_decode($response,true);
$access_token=$response['data']['access_token'];
$refresh_token=$response['data']['refresh_token'];
$expires_in=$response['data']['expires_in'];
$expiry_time=$response['data']['expiry_time'];
$data[0]['access_token']=$access_token;
$data[0]['expires_in']=$expires_in;
$data[0]['refresh_token']=$refresh_token;
$data[0]['expiry_time']=$expiry_time;
$d['amazonkey']=json_encode($data);
  $this->db1->where('id',$shopid);
  $this->db1->update('d_shop',$d);
//$this->db1->update("d_shop",array("amazonkey"=>$data),"id=".$shopid);
  // }
 // return $name;
  }









}
}






?>